package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/1/1 12:38
 * @description：https://leetcode-cn.com/problems/convert-1d-array-into-2d-array/
 */
public class 将一维数组转变成二维数组 {
    @Test
    public void test() {
        Solution solution = new Solution();
        int[][] ints = solution.construct2DArray(new int[]{1, 2}, 1, 1);
        for (int[] anInt : ints) {
            System.out.println(Arrays.toString(anInt));
        }
    }

    class Solution {
        public int[][] construct2DArray(int[] original, int m, int n) {
            if (original.length != m * n) return  new int[0][];
            int[][] ans = new int[m][n];
            for (int i = 0; i < original.length; i += n) {
                System.arraycopy(original, i, ans[i / n], 0, n);
            }
            return ans;
        }
    }
}
